#[cfg(test)]
mod tests {
    use super::super::dao::RoleDAO;
    use super::super::model::Role;
    use sqlx::{sqlite::SqlitePool};
    use std::sync::Arc;

    async fn setup_db() -> Arc<SqlitePool> {
        // 使用内存数据库进行测试
        let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
        
        // 创建角色表
        let create_table_sql = "CREATE TABLE IF NOT EXISTS roles (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            description TEXT NOT NULL
        )";
        sqlx::query(create_table_sql)
            .execute(&pool)
            .await
            .unwrap();

        Arc::new(pool)
    }

    #[tokio::test]
    async fn test_create_and_get_role() {
        let db = setup_db().await;
        let role_dao = RoleDAO::new(db);
        
        // 创建角色
        let role = Role {
            id: None,
            name: "Test Role".to_string(),
            description: "Test Role Description".to_string(),
        };
        
        let role_id = role_dao.create_role(&role).await.unwrap();
        assert!(role_id > 0);
        
        // 获取角色
        let fetched_role = role_dao.get_role(role_id).await.unwrap().unwrap();
        assert_eq!(fetched_role.name, "Test Role");
        assert_eq!(fetched_role.description, "Test Role Description");
    }

    #[tokio::test]
    async fn test_list_roles() {
        let db = setup_db().await;
        let role_dao = RoleDAO::new(db);
        
        // 创建几个测试角色
        let role1 = Role {
            id: None,
            name: "Role 1".to_string(),
            description: "Role 1 Description".to_string(),
        };
        
        let role2 = Role {
            id: None,
            name: "Role 2".to_string(),
            description: "Role 2 Description".to_string(),
        };
        
        role_dao.create_role(&role1).await.unwrap();
        role_dao.create_role(&role2).await.unwrap();
        
        // 获取所有角色
        let roles = role_dao.list_roles().await.unwrap();
        assert_eq!(roles.len(), 2);
    }

    #[tokio::test]
    async fn test_update_role() {
        let db = setup_db().await;
        let role_dao = RoleDAO::new(db);
        
        // 创建角色
        let role = Role {
            id: None,
            name: "Original Role".to_string(),
            description: "Original Description".to_string(),
        };
        
        let role_id = role_dao.create_role(&role).await.unwrap();
        
        // 更新角色
        let updated_role = Role {
            id: None,
            name: "Updated Role".to_string(),
            description: "Updated Description".to_string(),
        };
        
        let result = role_dao.update_role(role_id, &updated_role).await.unwrap();
        assert!(result);
        
        // 验证更新
        let fetched_role = role_dao.get_role(role_id).await.unwrap().unwrap();
        assert_eq!(fetched_role.name, "Updated Role");
        assert_eq!(fetched_role.description, "Updated Description");
    }

    #[tokio::test]
    async fn test_delete_role() {
        let db = setup_db().await;
        let role_dao = RoleDAO::new(db);
        
        // 创建角色
        let role = Role {
            id: None,
            name: "Test Role".to_string(),
            description: "Test Role Description".to_string(),
        };
        
        let role_id = role_dao.create_role(&role).await.unwrap();
        
        // 删除角色
        let result = role_dao.delete_role(role_id).await.unwrap();
        assert!(result);
        
        // 验证角色已删除
        let fetched_role = role_dao.get_role(role_id).await.unwrap();
        assert!(fetched_role.is_none());
    }

    #[tokio::test]
    async fn test_get_nonexistent_role() {
        let db = setup_db().await;
        let role_dao = RoleDAO::new(db);
        
        // 尝试获取不存在的角色
        let fetched_role = role_dao.get_role(999).await.unwrap();
        assert!(fetched_role.is_none());
    }

    #[tokio::test]
    async fn test_update_nonexistent_role() {
        let db = setup_db().await;
        let role_dao = RoleDAO::new(db);
        
        // 尝试更新不存在的角色
        let role = Role {
            id: None,
            name: "Test Role".to_string(),
            description: "Test Role Description".to_string(),
        };
        
        let result = role_dao.update_role(999, &role).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_delete_nonexistent_role() {
        let db = setup_db().await;
        let role_dao = RoleDAO::new(db);
        
        // 尝试删除不存在的角色
        let result = role_dao.delete_role(999).await.unwrap();
        assert!(!result);
    }
}